package test01.demo02.userOperation.controller;

import test01.demo02.userOperation.UserIfo.User;
import test01.demo02.userOperation.service.UserService;
import test01.demo02.userOperation.tools.MD5Encryption;

import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.UUID;

//表示层负责与用户交互，用户可以在这里选择是登录还是注册
public class UserController {

    UserService us = new UserService();

    Scanner scanner = new Scanner(System.in);

    /**
     * 注册
     */
    public void register() throws Exception {
        System.out.println("欢迎来到注册页面！");
        String userName;
        //从控制台获取用户名，判断用户名是否被使用过
        while (true){
            System.out.println("请输入用户名：");
            userName = scanner.nextLine();
            User findUser = us.findUserByName(userName);
            if (findUser!=null){
                System.out.println("用户名已经存在");
            }else {
                break;
            }
        }
        System.out.println("请输入密码：");
        String pwd = scanner.nextLine();

        System.out.println("请输入email：");
        String email = scanner.nextLine();

        //根据控制台的输入内容，封装一个User对象
        User user = new User(
                UUID.randomUUID().hashCode(),
                userName,
                MD5Encryption.getEncryption(pwd),
                email
        );
        //把对象传给Service层，执行注册操作
        boolean result = us.register(user);

        if (result){
            System.out.println("注册成功");
        }else {
            System.out.println("注册失败");
        }
    }

    /**
     * 登录
     */
    public void login() throws Exception {
        System.out.println("欢迎来到登录页面");

        while(true){
            System.out.println("请输入用户名：");
            String userName = scanner.nextLine();
            System.out.println("请输入密码：");
            String pwd = MD5Encryption.getEncryption(scanner.nextLine());
            //根据用户名和密码查找数据
            User findUser = us.login(userName,pwd);
            if (findUser!=null){
                System.out.println("登录成功");
                break;
            }else {
                System.out.println("登录失败");
            }
        }
    }
    //定义一个方法void update(),接收用户的用户名及邮件，根据用户名来修改邮箱，但要考虑用户是否真实存在
    public void update() throws Exception {
        System.out.println("欢迎来到修改界面");
        System.out.println("请输入要修改邮箱的用户名：");
        String userName = scanner.nextLine();
        while(true){
            User findUser = us.findUserByName(userName);
            if (findUser != null){
                System.out.println("请输入新的邮箱：");
                String email = scanner.nextLine();

                if (us.updateMethod(email,userName)){
                    System.out.println("邮箱修改成功！");
                    break;
                }else{
                    System.out.println("修改失败!");
                }
            }
        }
    }


    //定义一个方法static void delete(),接收用户输入的用户名，实现用户的删除功能
    public void delete(){
        System.out.println("欢迎来到删除界面");
        System.out.println("请输入要删除的用户名：");
        String userName = scanner.nextLine();
        while(true){
            User findUser = us.findUserByName(userName);
            if (findUser != null){
                System.out.println("开始删除了哈！");
                if (us.deleteMethod(userName)){
                    System.out.println("邮箱删除成功！");
                    break;
                }else{
                    System.out.println("删除失败!");
                }
            }
        }
    }

    public void select() throws Exception {
        System.out.println("欢迎来到查询界面");
        System.out.println("请输入要查询的用户名：");
        String userName = scanner.nextLine();
        us.selectMethod(userName);
    }

    public static void main(String[] args) throws Exception {
        UserController uc = new UserController();
        uc.select();
    }
}
